package com.selfdriving.dialog;

import java.io.File;
import com.selfdriving.R;
import com.selfdriving.utils.LogUtil;
import android.app.Activity;
import android.app.Dialog;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class ChoosePictureDialog extends Dialog implements OnClickListener {
	/**
	 * 拍照
	 */
	private LinearLayout tvChoosePicCapture;

	/**
	 * 媒体库
	 */
	private LinearLayout tvChoosePicSdcard;

	private ImageView ivCancel;

	private static Context context;

	public static final int CAMERA_WITH_DATA = 3023;

	// 用来标识请求gallery的activity
	public static final int PHOTO_PICKED_WITH_DATA = 3021;

	// 拍照的照片存储位置
	public static final File PHOTO_DIR = new File(
			Environment.getExternalStorageDirectory() + "/DCIM/Camera");

	public File mCurrentPhotoFile;// 照相机拍照得到的图片

	private Uri imageCaptureUri;

	public Uri getImageCaptureUri() {
		return imageCaptureUri;
	}

	public void setImageCaptureUri(Uri imageCaptureUri) {
		this.imageCaptureUri = imageCaptureUri;
	}

	private PhotoResultCallback callback = null;

	public static final int PICK_FROM_CAMERA = 1;
	public static final int CROP_FROM_CAMERA = 2;
	public static final int PICK_FROM_FILE = 3;
	public static final int CROP_FROM_FILE = 4;

	public static final boolean CROP_ENABLE = true;
	public static final boolean CROP_DISABLE = false;

	public static final boolean CHOOSE_FROM_FILE = true;
	public static final boolean CHOOSE_FROM_CAMERA = false;

	/**
	 * 是否開啟截圖模式，預設為開啟
	 */
	private boolean isCrop = CROP_ENABLE;

	public boolean isCrop() {
		return isCrop;
	}

	public void setCrop(boolean isCrop) {
		this.isCrop = isCrop;
	}

	public ChoosePictureDialog(Context context) {
		super(context, R.style.dialog);
		// 获得弹出窗口的布局
		this.context = context;
		setContentView(R.layout.group_choose_pic);
		findViews();
		setListener();
	}

	/**
	 * 设置监听
	 */
	private void setListener() {
		tvChoosePicCapture.setOnClickListener(this);
		tvChoosePicSdcard.setOnClickListener(this);
		ivCancel.setOnClickListener(this);
	}

	/**
	 * 资源
	 */
	private void findViews() {
		tvChoosePicCapture = (LinearLayout) findViewById(R.id.tvChoosePicCapture);
		tvChoosePicSdcard = (LinearLayout) findViewById(R.id.tvChoosePicSdcard);
		ivCancel = (ImageView) findViewById(R.id.tvChoosePicCancel);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tvChoosePicCapture:
			String status = Environment.getExternalStorageState();
			if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡
				getPhotoFromCamera();// 用户点击了从照相机获取
				dismiss();
			} else {
				Toast.makeText(context, "没有SD卡", Toast.LENGTH_SHORT).show();
			}
			break;
		case R.id.tvChoosePicSdcard:
			getImageFromGallery();// 从相册中去获取
			dismiss();
			break;
		case R.id.tvChoosePicCancel:
			dismiss();
			break;
		default:
			break;

		}
	}

	public static interface PhotoResultCallback {
		void onResult(Bitmap result);
	}

	public void fetchPhoto(PhotoResultCallback callback, boolean isFromGallery,
			boolean isCrop) {
		this.callback = callback;
		this.isCrop = isCrop;
		if (isFromGallery) {
			getImageFromGallery();// 从相册中去获取
			dismiss();
		} else {
			String status = Environment.getExternalStorageState();
			if (status.equals(Environment.MEDIA_MOUNTED)) {// 判断是否有SD卡
				getPhotoFromCamera();// 用户点击了从照相机获取
				dismiss();
			} else {
				Toast.makeText(context, "没有SD卡", Toast.LENGTH_SHORT).show();
			}
		}
	}

	protected void getImageFromGallery() {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		((Activity) context).startActivityForResult(
				Intent.createChooser(intent, "Complete action using"),
				PICK_FROM_FILE);
	}

	protected void getPhotoFromCamera() {
		Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

		imageCaptureUri = Uri.fromFile(new File(Environment
				.getExternalStorageDirectory(), "tmp_"
				+ String.valueOf(System.currentTimeMillis()) + ".jpg"));

		intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
				imageCaptureUri);
		LogUtil.e("imageCaptureUri:" + imageCaptureUri);
		try {
			intent.putExtra("return-data", true);
			((Activity) context).startActivityForResult(intent,
					ChoosePictureDialog.PICK_FROM_CAMERA);
		} catch (ActivityNotFoundException e) {
			e.printStackTrace();
		}
	}


	/**
	 * 
	 * @param mCurrentPhotoFile2
	 */
	public void doCropPhoto(File mCurrentPhotoFile2) {

	}

	public static String getRealPathFromURI(Uri contentUri) {
		String res = null;
		if (contentUri != null) {
			String[] proj = { MediaStore.Images.Media.DATA };
			Cursor cursor = context.getContentResolver().query(contentUri,
					proj, null, null, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					int column_index = cursor
							.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
					res = cursor.getString(column_index);
				}
				cursor.close();
			}
		}
		return res;
	}
}
